package com.asj.house.schedule;

import com.asj.house.entity.HouseSubscribe;
import com.asj.house.service.HouseSubscribeService;
import com.asj.house.utils.TimeUtilCustomize;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ScanSubscribeJob {

    @Autowired
    private HouseSubscribeService houseSubscribeService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void updateType() {
        String today = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_DAY_FORMAT, new Date());
        QueryWrapper<HouseSubscribe> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseSubscribe::getTime, today)
                .eq(HouseSubscribe::getType, 0)
                .eq(HouseSubscribe::getDeleted, 0);
        List<HouseSubscribe> houseSubscribeList = houseSubscribeService.list(queryWrapper);

        if (houseSubscribeList.isEmpty()) {
            log.info("未发现今日预约数据");
            return;
        }

        houseSubscribeList.forEach(houseSubscribe -> {
            houseSubscribe.setType(2);
        });

        houseSubscribeService.updateBatchById(houseSubscribeList);
        log.info("今日预约数据修改成功");
    }

}
